package org.example.main.service.impl;

import org.apache.log4j.Logger;
import org.example.main.dao.RecordMapper;
import org.example.main.entity.Record;
import org.example.main.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    private static final Logger logger = Logger.getLogger(RecordServiceImpl.class);

    @Autowired
    private RecordMapper recordMapper;

    @Override
    public int add(Record record){
        logger.info("Adding new record for house ID: " + record.getHouseId() + " and tenant ID: " + record.getTenantId());
        int result = recordMapper.insert(record);
        logger.info("Record added with ID: " + record.getRecordId() + ", result: " + result);
        return result;
    }

    @Override
    public int deleteById(long recordId){
        logger.info("Deleting record by ID: " + recordId);
        int result = recordMapper.deleteById(recordId);
        logger.info("Record deletion result: " + result);
        return result;
    }

    @Override
    public List<Record> findAll(){
        logger.info("Retrieving all records");
        List<Record> records = recordMapper.findAll();
        logger.debug("Found " + records.size() + " records");
        return records;
    }

    @Override
    public List<Record> findByHouseId(long houseId){
        logger.info("Finding records by house ID: " + houseId);
        List<Record> records = recordMapper.findByHouseId(houseId);
        logger.debug("Found " + records.size() + " records for house ID: " + houseId);
        return records;
    }

    @Override
    public List<Record> findByTenantId(long tenantId){
        logger.info("Finding records by tenant ID: " + tenantId);
        List<Record> records = recordMapper.findByTenantId(tenantId);
        logger.debug("Found " + records.size() + " records for tenant ID: " + tenantId);
        return records;
    }
}
